// mircoApp/shopsProfits/profits/profits
import util from "../../../utils/util";
var app = getApp();
const skinBehavior = require('../../../utils/themeSkin/skinBehavior');
Page({
  behaviors: [skinBehavior],
  /**
	 * 页面的初始数据
	 */
  data: {
    timer:'',
    buyTimer:'',
    uploadUrl: "upload.uploadPic", //上传图片
    theme_color:'',
    username: '',
    stopButton:false,
    language:'',
    bannerInfo:{},
    query:{
      orders: JSON.stringify([]),
      member_coupon_ids:[],
      address:'',
      store_profit_price:'',
      store_profit_uid:'',
      store_profit_image:''
    }
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    this.initData();
    this.profitsIndex();
  },
  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {
    const childComponent = this.selectComponent('#myChild');
    childComponent.getData();
    let language = wx.getStorageSync("langIndex");
    this.setData({
      language: language.en
    });
  },

  /**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function() {
  },

  /**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function() {

  },

  /**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function() {

  },

  /**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function() {

  },

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function() {

  },

  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function() {

  },
  getMembername() { //搜索会员
    let that = this;
    let json = {
      store_profit_uid: this.data.query.store_profit_uid
    };
    let urlStr = app.getNetAddresss("plugin.store-profit.frontend.index.getMemberNickname");
    app._getNetWork({
      url: urlStr,
      data:json,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            username: res.data.username
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  profitsIndex() { //banner详情
    let urlStr = app.getNetAddresss("plugin.store-profit.frontend.index.index");
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          this.setData({
            bannerInfo: res.data
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  bannerTo(event) {
    let minijump = event.currentTarget.dataset.minijump;
    if(minijump){
      wx.navigateTo({
        url: minijump,
      })
    }
  },
  //受让人ID的值绑定
  storeChange(e) {
    if(e.detail.value == '') return false;
    let val = e.detail.value;
    this.setData({
      'query.store_profit_uid': val
    });
    if (this.data.timer) clearTimeout(this.data.timer);
    this.timer = setTimeout(() => {
      util.debounce(this.getMembername());
    }, 500);
  },
  //转账的值绑定
  priceCange(e) {
    let val = e.detail.value;
    this.setData({
      'query.store_profit_price': val.replace(/^(\-)*(\d+)\.(\d\d).*$/, "$1$2.$3")
    });
  },
  removeImg() {
    this.setData({
      'query.store_profit_image':''
    })
  },
  initData() {
    let query = {
      orders: JSON.stringify([]),
      member_coupon_ids:[],
      address:'',
      store_profit_price:'',
      store_profit_uid:'',
      store_profit_image:''
    };
    this.setData({
      username:'',
      query:query
    });
  },
  onRead(event) { //图片上传功能
    let itemName = event.currentTarget.dataset.itemname;
    let that = this;
    let urlStr = app.getNetAddresss(this.data.uploadUrl);
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const tempFilePaths = res.tempFilePaths;
        let photourl = tempFilePaths[0];
        wx.uploadFile({
          url: urlStr,
          filePath: photourl,
          name: 'file',
          success(resdata) {
            let res = JSON.parse(resdata.data);
            if (res.result == 1) {
              that.setData({
                [itemName]: res.data.img_url
              });
            } else {
              wx.showToast({
                icon: 'none',
                title: res.msg,
                duration: 1500
              });
            }
          }
        });
      }
    });
  },
  //确认转账
  profitsConfirm() {
    if(app._isTextEmpty(this.data.username)) {
      wx.showToast({
        icon: "none",
        title: "会员不存在",
        duration: 1000,
      });
      return false;
    }
    if(app._isTextEmpty(this.data.query.store_profit_uid)) {
      wx.showToast({
        icon: "none",
        title: "让利会员ID错误",
        duration: 1000,
      });
      return false;
    }
    if(app._isTextEmpty(this.data.query.store_profit_price)) {
      wx.showToast({
        icon: "none",
        title: "请输入让利价格",
        duration: 1000,
      });
      return false;
    }
    this.data.query.store_profit_price = parseFloat(this.data.query.store_profit_price);
    if (this.data.buyTimer) clearTimeout(this.data.buyTimer);
      this.data.buyTimer = setTimeout(() => {
        let profits = {uid:this.data.query.store_profit_uid,price:this.data.query.store_profit_price,image:this.data.query.store_profit_image};
        wx.setStorageSync('profits', JSON.stringify(profits));
        wx.navigateTo({
          url: '/mircoApp/shopsProfits/profitsPay/profitsPay'
        })
      }, 300);
  }
});
